<?php
/**
 * author:wang
 * mail:970633164@qq.com
 */

define("PHP_IN",true);
require "include/init.php";

if(isset($_POST['dosubmit'])){
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    $imgcode = trim($_POST['imgcode']);
    session_start();
    if(!isset($_SESSION['imgcode']) || $_SESSION['imgcode'] != $imgcode){
        alert("验证码错误！");
        exit;
    }
    if($username=="" || $password==""){
        alert("用户名密码不能为空");
        exit;
    }
    $password_md5 = md5($password);
    $sql = "SELECT * FROM user WHERE username = '$username' AND password = '$password_md5' LIMIT 1";
    $result = $db->query($sql);
    if($result && $db->num_rows($result)){
        $user = $db->fetch_one($result);
        //setcookie("user",$user);
        setcookie("userid",$user['userid']);
        setcookie("password",$user['password']);
        header("Location:user_center.php");
    }else{
        alert("该用户名密码不正确");
        exit;
    }
}

$smarty->setTemplateDir(ROOT_PATH."templates");        //配置smarty
$smarty->setCompileDir(ROOT_PATH."templates/templates_c");
$smarty->setLeftDelimiter("<{");
$smarty->setRightDelimiter("}>");

$smarty->display("user_login.tpl");
$db->close();

?>
